<?php


namespace App\Channel;


use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Log;

class WechatChannel
{
    /**
     * 发送指定的通知.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return void
     */
    public function send($notifiable, Notification $notification)
    {
        $message = $notification->toWechat($notifiable);

       if (config('app.env')=='local'){
            Log::channel('wechat')->info('推送成功1:'.$notifiable->name,$message->toArray());
            return true;
        }
        if (!$notifiable->userid){
            Log::channel('wechat')->error('推送失败:'.$notifiable->name,$message->toArray());
        }
        $app= \EasyWeChat::work();
        try {
            $result=$app->messenger->ofAgent('1000011')->message($message)->toUser($notifiable->userid)->send();

            if (isset($result['errcode'])&&$result['errcode']===0){
                Log::channel('wechat')->info('推送成功2:'.$notifiable->name,$message->toArray());
            }else{
                Log::channel('wechat')->error('推送失败3:',$notifiable->toArray());
            }

        }catch (\Exception $exception){
            Log::channel('wechat')->error('推送失败3:'.$exception->getMessage(),$notifiable->toArray());
        }
    }
}
